package pl.prv.polanie.client.filemanager;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Janusz Prokulewicz
 */

public class Directory extends Composite implements NestedElement {
	
	private static final String STYLE__DIRECTORY_CONTAINER = "swo-DirectoryContainer";
	
	private static final String STYLE__NO_ITEMS_LABEL = "noItemsLabel";
	
	private static final String STYLE__CHILDREN = "children";
	
	
	private static final String STYLE__CROSS_COLLAPSED = "cross-collapsed";
	private static final String STYLE__CROSS_EXPANDED = "cross-expanded";
	

	
	private static final String STYLE__DIR_ICON_OPENED = "diricon-opened";
	private static final String STYLE__DIR_ICON_CLOSED = "diricon-closed";
	private static final String STYLE__DIR_ICON_IMAGES = "diricon-images";
	
	
	//rodzaje ikon
	private static final int ICON_TYPE_OPENED = 0;
	private static final int ICON_TYPE_CLOSED = 1;
	private static final int ICON_TYPE_IMAGES = 2;
	
	
	//zamkniete, otwarte, puste
	private static final int CROSS_TYPE_CLOSED = 0;
	private static final int CROSS_TYPE_OPENED = 1;
	private static final int CROSS_TYPE_EMPTY = 2;
	
	
	
	//flaga wskazujaca czy dodano juz jakis element
	private boolean empty = true;
	
	
	
	private VerticalPanel mainVP = new VerticalPanel();
	
	
	//
	private HorizontalPanel dirHeaderHP = new HorizontalPanel();
	// |
	private HTML dirCross = new HTML();
	private HTML dirIcon = new HTML();	
	private HTML dirName = new HTML("domyslna nazwa katalogu");
	
	
	private VerticalPanel childrenVP = new VerticalPanel();
	
	private String path = null;
	
	//logika
	boolean collapsed = true;
	
	private Object userObject = null;
	
	public Directory(String dirName) {

		setupChildrenVP();
		setupHeaderHP();
		
		
		mainVP.setStyleName(STYLE__DIRECTORY_CONTAINER);
		
		mainVP.add(dirHeaderHP);
		mainVP.add(childrenVP);
		
		initWidget(mainVP);
		
		
		setCollapsed(true);
		setIcon(ICON_TYPE_CLOSED);
	}
	
	public Directory() {
		this("");
	}

	
	
	
	
	
	private void setupChildrenVP() {
		childrenVP.setStyleName(STYLE__CHILDREN);
		
		Label noFilesLabel = new Label("-- no files --");
		noFilesLabel.setStyleName(STYLE__NO_ITEMS_LABEL);
		
		childrenVP.add(noFilesLabel);
	}
	
	private void setupHeaderHP() {
		dirHeaderHP.setHeight("14px");
		
		dirHeaderHP.add(dirCross);
		dirHeaderHP.add(dirIcon);
		dirHeaderHP.add(dirName);
		
		dirCross.setWidth("20px");
		dirCross.setHeight("16px");
		dirCross.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				Directory.this.setCollapsed(!Directory.this.collapsed);
			}
		});
		
		dirIcon.setWidth(20+"px");
		dirIcon.setHeight(16+"px");
	}
	
	public void addExampleData() {
//		addChild(new Directory());
//		addChild(new File("plik 1"));
//		addChild(new File("plik 2"));
//		addChild(new File("plik 3"));
//		addChild(new File("plik 4"));
//		addChild(new File("plik 5"));
//		addChild(new File("plik 6"));
//		addChild(new File("plik 7"));
//		addChild(new File("plik 8"));

	}
	
	public void addChild(NestedElement child) {
		if (child instanceof Widget) {
			if (empty) {
				childrenVP.clear();
				empty = false;
			}
			Widget w = (Widget) child;
			childrenVP.add(w);	
		}
	}
	
	public void setCollapsed(boolean collapsed) {
		this.collapsed = collapsed;
		if (collapsed) {
			dirCross.setStyleName(STYLE__CROSS_COLLAPSED);
			childrenVP.setVisible(false);
		} else {
			dirCross.setStyleName(STYLE__CROSS_EXPANDED);
			childrenVP.setVisible(true);
		}
	}
	
	public void setCollapsedRecursively(boolean collapsed) {
		setCollapsed(collapsed);
		for (int i = 0; i < childrenVP.getWidgetCount(); i++) {
			Widget w = childrenVP.getWidget(i);
			if (w instanceof Directory) {
				Directory d = (Directory) w;
				d.setCollapsedRecursively(collapsed);
			}
		}
	}
	
	public void setIcon(int dirIconConst) {
		switch (dirIconConst) {
		case ICON_TYPE_CLOSED:
			this.dirIcon.setStyleName(STYLE__DIR_ICON_CLOSED);
			break;
		case ICON_TYPE_OPENED:
			this.dirIcon.setStyleName(STYLE__DIR_ICON_OPENED);
			break;
		case ICON_TYPE_IMAGES:
			this.dirIcon.setStyleName(STYLE__DIR_ICON_IMAGES);
			break;

		default:
			break;
		}
	}



	public boolean isEmpty() {
		return empty;
	}
	
	public void setDirName(String text) {
		dirName.setText(text);
	}
	
	public String getDirName() {
		return dirName.getText();
	}



	public String getPath() {
		return path;
	}



	public void setPath(String path) {
		this.path = path;
	}



	public Object getUserObject() {
		return userObject;
	}



	public void setUserObject(Object userObject) {
		this.userObject = userObject;
	}
	
	
}
